package com.xltt.newbleclient.bt;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import com.google.gson.Gson;
import com.xltt.byteutil.User;
import com.xltt.newbleclient.R;
import com.xltt.newbleclient.ble.User2;
import com.xltt.newbleclient.utils.CProgressDialogUtils;
import com.xltt.newbleclient.utils.HProgressDialogUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class BluetoothActivity extends AppCompatActivity implements BluetoothReceiver.OnBluetoothReceiverListener,BluetoothListAdapter.Listener,BaseBluetooth.BTListener {

    private BluetoothAdapter mBluetoothAdapter;
    private BluetoothReceiver mBluetoothReceiver;
    private ListView mBtListView;
    private BluetoothListAdapter mBluetoothListAdapter;
    private BluetoothClient mBluetoothClient;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bt_activity);
        mBtListView = findViewById(R.id.bt_list);
        mBluetoothListAdapter = new BluetoothListAdapter(this,this);
        mBtListView.setAdapter(mBluetoothListAdapter);
        //打开蓝牙广播接受
        mBluetoothReceiver = new BluetoothReceiver(this, this);//注册蓝牙广播
        //打开蓝牙开关
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mBluetoothClient = new BluetoothClient(this);
        startDiscovery();
    }

    private void startDiscovery(){
        if (!mBluetoothAdapter.isEnabled()) {
            //开启蓝牙，耗时，监听广播进行后续操作
            mBluetoothAdapter.enable();
        }
        mBluetoothAdapter.startDiscovery();
    }

    public void clickView(View view){
        if(view.getId() == R.id.btnStart){
            mBluetoothListAdapter.removeAllBluetoothDevice();
            startDiscovery();
        }else if(view.getId() == R.id.btnStop){
            if(mBluetoothAdapter != null){
                mBluetoothAdapter.cancelDiscovery();
            }
        }else if(view.getId() == R.id.send_data){
            User2 user = new User2("987654321", "qwer1234",1);
            String json = new Gson().toJson(user);
            mBluetoothClient.sendMsg(json);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mBluetoothAdapter != null){
            mBluetoothAdapter.cancelDiscovery();
        }
        if(mBluetoothReceiver != null){
            unregisterReceiver(mBluetoothReceiver);
        }
    }

    @Override
    public void onDeviceFound(BluetoothDevice bluetoothDevice) {
        mBluetoothListAdapter.addBluetoothDevice(bluetoothDevice);
    }

    @Override
    public void onScanFinish() {

    }

    @Override
    public void onStateChanged(int state) {
        if(state == BluetoothAdapter.STATE_ON){
            Log.d("zhanghui","蓝牙已经打开");
            //蓝牙扫描
            if(mBluetoothAdapter != null){
                mBluetoothAdapter.startDiscovery();
            }
        }else if(state == BluetoothAdapter.STATE_OFF){
            Log.d("zhanghui","蓝牙已经关闭");
            if(mBluetoothAdapter != null){
                mBluetoothAdapter.cancelDiscovery();
            }
            mBluetoothListAdapter.removeAllBluetoothDevice();
        }

    }

    @Override
    public void onItemClick(BluetoothDevice dev) {
        Log.d("zhanghui","点击连接 = " + dev.getName() + ",地址 = " + dev.getAddress());
        if(mBluetoothClient != null){
            mBluetoothClient.connect(dev);
        }
        CProgressDialogUtils.showProgressDialog(this);
    }

    @Override
    public void socketNotify(int state, Object obj) {
        if(state == BaseBluetooth.BTListener.CONNECTED){
            Log.d("zhanghui","连接成功");
        }else if(state == BaseBluetooth.BTListener.DISCONNECTED){
            Log.d("zhanghui","连接断开");
        }else if(state == BaseBluetooth.BTListener.MSG_SEND){
            Log.d("zhanghui","发送消息");
        }else if(state == BaseBluetooth.BTListener.MSG_RECEIVED){
            Log.d("zhanghui","接受消息");
        }
        CProgressDialogUtils.cancelProgressDialog();
    }
}
